﻿using Dome.Card.Entitys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dome.Card.EFCore
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> option) : base(option) { }
        public DbSet<comments> comments { get; set; }
        public DbSet<leaseHistroy> leaseHistroys { get; set; }
        public DbSet<owner> owners { get; set; }
        public DbSet<park> parks { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //评论表
            modelBuilder.Entity<comments>().HasOne(o => o.owner).WithMany(o=>o.comments).HasForeignKey(o=>o.ownerId).OnDelete(DeleteBehavior.NoAction); 
            modelBuilder.Entity<comments>().HasOne(o=>o.park).WithMany(o=>o.comments).HasForeignKey(o=>o.parkId).OnDelete(DeleteBehavior.NoAction); 
            modelBuilder.Entity<comments>(o => {
                o.HasQueryFilter(o => !o.isdelete);
            });
            //历史出租表
            modelBuilder.Entity<leaseHistroy>().HasOne(o => o.owner).WithMany(o => o.leaseHistroys).HasForeignKey(o => o.ownerId).OnDelete(DeleteBehavior.NoAction); 
            modelBuilder.Entity<leaseHistroy>().HasOne(o => o.park).WithMany(o => o.leaseHistroys).HasForeignKey(o => o.parkId).OnDelete(DeleteBehavior.NoAction); 
            //车位表
            modelBuilder.Entity<park>().HasOne(o => o.owner).WithMany(o => o.parks).HasForeignKey(o => o.ownerId).OnDelete(DeleteBehavior.NoAction);
            //业主表
            modelBuilder.Entity<owner>(o => {
                o.HasQueryFilter(o=>!o.isdelete);
            });
        }
    }
}
